﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ChessEngine;
using IChessEngine;
using IBLL;
using BLL;
using Microsoft.Win32;

namespace ChessTrainer
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private IChsGameEditBLL bll = new ChsXmlGameEditBLL();
        private IChsGame game = null;
        public MainWindow()
        {
            InitializeComponent();
            //Game.Game = new ChessEngine.Gaming.ChsGame();
        }
        private void New_Click(object sender, RoutedEventArgs e)
        {
            SaveFileDialog dlg = new SaveFileDialog();
            bool? result = dlg.ShowDialog();
            if (result.HasValue && result.Value)
            {
                bll.OpenDAO(dlg.FileName);
                GamesList.ItemsSource = bll.GetGames();
                GamesList.Visibility = Visibility.Visible;
                MenuNew.IsEnabled = false;
                MenuOpen.IsEnabled = false;
                MenuClose.IsEnabled = true;
                MenuSave.IsEnabled = true;
                MenuNewGame.IsEnabled = true;
                MenuSaveGame.IsEnabled = true;
            }
        }
        private void Open_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            bool? result = dlg.ShowDialog();
            if (result.HasValue && result.Value)
            {
                bll.OpenDAO(dlg.FileName);
                GamesList.ItemsSource = bll.GetGames();
                GamesList.Visibility = Visibility.Visible;
                MenuNew.IsEnabled = false;
                MenuOpen.IsEnabled = false;
                MenuClose.IsEnabled = true;
                MenuSave.IsEnabled = true;
                MenuNewGame.IsEnabled = true;
                MenuSaveGame.IsEnabled = true;
            }
        }
        private void Save_Click(object sender, RoutedEventArgs e)
        {
            if (GamesList.Visibility == Visibility.Visible)
            {
                bll.Save();
            }
        }
        private void Close_Click(object sender, RoutedEventArgs e)
        {
            if (GamesList.Visibility == Visibility.Visible)
            {
                bll.CloseDAO();
                GamesList.ItemsSource = null;
                GamesList.Visibility = Visibility.Hidden;
                MenuNew.IsEnabled = true;
                MenuOpen.IsEnabled = true;
                MenuClose.IsEnabled = false;
                MenuSave.IsEnabled = false;
                MenuNewGame.IsEnabled = false;
                MenuSaveGame.IsEnabled = false;
            }
        }
        private void NewGame_Click(object sender, RoutedEventArgs e)
        {
            Game.Visibility = Visibility.Visible;
            game = bll.CreateGame("blanco", "negro", DateTime.Now);
            Game.SetBll(bll, game.Id);
            ResizeControls();
        }
        private void SaveGame_Click(object sender, RoutedEventArgs e)
        {
            bll.SaveGame(game.Id);
        }
        private void ResizeControls()
        {
            this.Width = Game.Width + GamesList.Width;
        }
    }
}
